package com.cleaningcloud.cld.controller;

import com.cleaningcloud.cld.domain.CldInvoice;
import com.cleaningcloud.cld.domain.dto.CldInvoiceDto;
import com.cleaningcloud.cld.domain.vo.CldInvoiceVo;
import com.cleaningcloud.cld.service.ICldInvoiceService;
import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发票管理Controller
 *
 * @author cleaningcloud
 * @date 2024-10-16
 */
@RestController
@RequestMapping("/invoice")
public class CldInvoiceController extends BaseController {
    @Autowired
    private ICldInvoiceService cldInvoiceService;

    /**
     * 查询发票管理列表
     */
    @RequiresPermissions("cld:invoice:list")
    @GetMapping("/list")
    public TableDataInfo list(CldInvoiceDto cldInvoice) {
        startPage();
        List<CldInvoiceVo> list = cldInvoiceService.selectCldInvoiceList(cldInvoice);
        return getDataTable(list);
    }


    /**
     * 按项目区域分组统计
     */
    @RequiresPermissions("cld:invoice:list")
    @GetMapping("/groupByRegionList")
    public TableDataInfo groupByRegionList(CldInvoiceDto cldInvoice) {
        startPage();
        List<CldInvoiceVo> list = cldInvoiceService.selectCldInvoiceGroupRegionList(cldInvoice);
        return getDataTable(list);
    }

    /**
     * 导出发票管理列表
     */
    @RequiresPermissions("cld:invoice:export")
    @Log(title = "发票管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CldInvoiceDto cldInvoice) {
        List<CldInvoiceVo> list = cldInvoiceService.selectCldInvoiceList(cldInvoice);
        ExcelUtil<CldInvoiceVo> util = new ExcelUtil<CldInvoiceVo>(CldInvoiceVo.class);
        util.exportExcel(response, list, "发票管理数据");
    }

    /**
     * 获取发票管理详细信息
     */
    @RequiresPermissions("cld:invoice:query")
    @GetMapping(value = "/{invoiceId}")
    public AjaxResult getInfo(@PathVariable("invoiceId") Long invoiceId) {
        return success(cldInvoiceService.selectCldInvoiceByInvoiceId(invoiceId));
    }

    /**
     * 新增发票管理
     */
    @RequiresPermissions("cld:invoice:add")
    @Log(title = "发票管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CldInvoice cldInvoice) {
        return toAjax(cldInvoiceService.insertCldInvoice(cldInvoice));
    }

    /**
     * 修改发票管理
     */
    @RequiresPermissions("cld:invoice:edit")
    @Log(title = "发票管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CldInvoice cldInvoice) {
        return toAjax(cldInvoiceService.updateCldInvoice(cldInvoice));
    }

    /**
     * 删除发票管理
     */
    @RequiresPermissions("cld:invoice:remove")
    @Log(title = "发票管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{invoiceIds}")
    public AjaxResult remove(@PathVariable Long[] invoiceIds) {
        return toAjax(cldInvoiceService.deleteCldInvoiceByInvoiceIds(invoiceIds));
    }


    /**
     * 发票头部统计
     */
    @GetMapping("/selectCldInvoiceStatistics")
    public AjaxResult selectCldInvoiceStatistics(CldInvoiceDto cldInvoice) {
        return success(cldInvoiceService.selectCldInvoiceStatistics(cldInvoice));
    }
}
